﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Kooboo.Commerce.AddIn;


namespace Kooboo.Commerce.Validators
{
    [Export(typeof(IValidator))]
    public class Range : IValidator
    {
        public string Name
        {
            get
            {
                return "Range";
            }
        }

        public bool Validate(string value, List<ValidatorParameter> args)
        {
            var maximum = args.Where(i => i.Name == "Maximum").Select(i=>i.Value).FirstOrDefault();
            var minimum = args.Where(i => i.Name == "Minimum").Select(i => i.Value).FirstOrDefault();


            if (string.IsNullOrWhiteSpace(maximum) == false)
            {
                decimal number;
                DateTime date;
                if (decimal.TryParse(maximum, out number))
                {
                    try
                    {
                        if (number < decimal.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else if (DateTime.TryParse(maximum, out date))
                {
                    try
                    {
                        if (date < DateTime.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {

                    if(maximum == value)
                    {
                        return false;
                    }
                    else
                    {

                        List<string> values = (new List<string>()
                        {
                            maximum,
                            value
                        })
                        .OrderBy(i => i)
                        .ToList();

                        if (values[0] == maximum)
                        {
                            return false;
                        }
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(minimum) == false)
            {
                decimal number;
                DateTime date;
                if (decimal.TryParse(minimum, out number))
                {
                    try
                    {
                        if (number > decimal.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else if (DateTime.TryParse(minimum, out date))
                {
                    try
                    {
                        if (date > DateTime.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {

                    if (minimum == value)
                    {
                        return false;
                    }
                    else
                    {

                        List<string> values = (new List<string>()
                        {
                            minimum,
                            value
                        })
                        .OrderBy(i => i)
                        .ToList();

                        if (values[1] == minimum)
                        {
                            return false;
                        }
                    }
                }
            }


            return true;

        }

        public List<ValidatorParameter> GetParameters()
        {

            return new List<ValidatorParameter>()
            {
                new ValidatorParameter{ Name = "Minimum", Example = "(0)"},
                new ValidatorParameter{ Name = "Maximum", Example = "(100)"}                
            };

        }
    }
}